import Vue from 'vue'
import Router from 'vue-router'

// 使用router插件，会在全局上注册router-view和router-link全局组件
// 会为vue扩展一个属性$router和$route
Vue.use(Router)

/* Layout */
// 引入Layout布局的页面
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
// 静态路由表(路由规则对象数组)
export const constantRoutes = [
  // 登录页
  {
    path: '/login', // 路径
    // 问题:
    // 如果都是在上面import一起引入这些页面组件
    // 在webpack打包代码的时候，会一起打包进app.js中，然后插入到index.html执行
    // 这样就导致了首屏加载了一些本来不需要显示的其他页面组件的(相关代码)
    // 导致首屏加载过慢I
    // 解决方案:
    // webpack提供了import导入函数，可以去导入对应的模块，原地返回的是一个promise

    // 写法
    // component:()=>import('组件文件的路径')

    // 在webpack打包代码的时候，就会给这些组件分成多个js文件，而app.js里代码会变少
    // 这样导致首屏只会加载对应一个页面的js代码(加速首屏打开速度)
    component: () => import('@/views/login/index'), // 跳转组件
    hidden: true // 控制侧边栏是否显示导航路径
  },
  // 404页
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  // 首页
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard', // 默认打开的项目，默认路由地址/，立即改变url地址栏路由地址(会重新进行路由表的path匹配)
    children: [{ // 子路由
      path: 'dashboard',
      name: 'Dashboard',
      // webpackChunkName用来定义懒加载时dist下的js文件名
      component: () => import(/* webpackChunkName: "Login"*/ '@/views/dashboard/index'), // 挂载layout右下角(AppMain.vue中)
      meta: { title: 'Dashboard', icon: 'dashboard' } // meta固定名字，用来给当前路由对象添加额外信息(meta用于生成左侧菜单项的标题和图形)
    }]
  },
  //
  {
    path: '/example', // 只有一级路由的路径需要写/，子级路由不需要写
    component: Layout,
    redirect: '/example/table',
    name: 'Example',
    meta: { title: 'Example', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'table',
        name: 'Table',
        component: () => import('@/views/table/index'),
        meta: { title: 'Table', icon: 'table' }
      },
      {
        path: 'tree',
        name: 'Tree',
        component: () => import('@/views/tree/index'),
        meta: { title: 'Tree', icon: 'tree' }
      }
    ]
  },

  {
    path: '/form',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'Form',
        component: () => import('@/views/form/index'),
        meta: { title: 'Form', icon: 'form' }
      }
    ]
  },

  {
    path: '/nested',
    component: Layout,
    redirect: '/nested/menu1',
    name: 'Nested',
    meta: {
      title: 'Nested',
      icon: 'nested'
    },
    children: [
      {
        path: 'menu1',
        component: () => import('@/views/nested/menu1/index'), // Parent router-view
        name: 'Menu1',
        meta: { title: 'Menu1' },
        children: [
          {
            path: 'menu1-1',
            component: () => import('@/views/nested/menu1/menu1-1'),
            name: 'Menu1-1',
            meta: { title: 'Menu1-1' }
          },
          {
            path: 'menu1-2',
            component: () => import('@/views/nested/menu1/menu1-2'),
            name: 'Menu1-2',
            meta: { title: 'Menu1-2' },
            children: [
              {
                path: 'menu1-2-1',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-1'),
                name: 'Menu1-2-1',
                meta: { title: 'Menu1-2-1' }
              },
              {
                path: 'menu1-2-2',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-2'),
                name: 'Menu1-2-2',
                meta: { title: 'Menu1-2-2' }
              }
            ]
          },
          {
            path: 'menu1-3',
            component: () => import('@/views/nested/menu1/menu1-3'),
            name: 'Menu1-3',
            meta: { title: 'Menu1-3' }
          }
        ]
      },
      {
        path: 'menu2',
        component: () => import('@/views/nested/menu2/index'),
        name: 'Menu2',
        meta: { title: 'menu2' }
      }
    ]
  },

  {
    path: 'external-link',
    component: Layout,
    children: [
      {
        path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
        meta: { title: 'External Link', icon: 'link' }
      }
    ]
  },

  // 路由匹配顺序从上到下，最后设置完全匹配不到的路由信息
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes // 传入路由规则数组(注册路由表)
})

const router = createRouter() // 路由对象

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 最后权限设计会使用这个方法
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router // 路由对象导出
